package com.baijiaoxi.common.model.excel;

import com.baijiaoxi.common.model.base.service.extension.FieldAccessByNameService;
import com.baijiaoxi.common.model.base.service.extension.FieldSetByNameUtil;
import com.baijiaoxi.common.utils.jdk.Strings;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public interface ExcelImportRowHandler<T> {

    default FieldAccessByNameService getFieldAccessibleByNameService() {
        return null;
    }

    default Class getClassType() {
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        Class tClass = (Class) actualTypeArguments[0];
        return tClass;

    }

    T newInstance();

    default T setCellValue(T entity, HeaderCellInfo headerCellInfo, String value) {
        return Strings.isBlank(headerCellInfo.getFieldName()) ?
                setCellValue(entity, headerCellInfo.getHeaderIndex(), headerCellInfo.getHeaderTitle(), value)
                : setCellValue(entity, headerCellInfo.getFieldName(), value);
    }

    default T setCellValue(T entity, int headerIndex, String headerTitle, String value) {
        return entity;
    }

    default T setCellValue(T entity, String fieldName, String value) {
        FieldAccessByNameService fieldAccessibleByNameService = getFieldAccessibleByNameService();
        if (fieldAccessibleByNameService != null)
            fieldAccessibleByNameService.setFieldValue(entity, fieldName, value);
        else FieldSetByNameUtil.setFieldValue(entity, fieldName, value);
        return entity;
    }

    default void afterAnalyseRow(T row, List<HeaderCellInfo> headerHeaderCellInfoList) {
        afterAnalyseRow(row);
    }

    void afterAnalyseRow(T row);

}
